<template>
    <div :style ="note">
        <el-form ref="loginForm" :rules="loginFormRules" :model="loginForm" class="loginPage">
            <h3 class="loginTitle">系统登录</h3>
            <el-form-item prop="userName">
                <el-input type="text" auto-complete="false" v-model="loginForm.userName" @keyup.enter.native="submitLogin" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" auto-complete="false" v-model="loginForm.password" @keyup.enter.native="submitLogin" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item prop="code">
                <el-input type="text" auto-complete="false" v-model="loginForm.code" @keyup.enter.native="submitLogin" placeholder="请输入验证码" style="width: 250px;margin-right: 5px"></el-input>
                <img :src="captchaUrl" @click="getCaptcha">
            </el-form-item>
            <el-checkbox v-model="checked" class="loginRemember">记住我</el-checkbox>
            <el-button type="primary" style="width: 100%" @click="submitLogin">登录</el-button>
        </el-form>
    </div>

</template>
<script>
    import {getCaptchaImage,login,getInfo} from '@/api/login'
    export default {
        name: "Login",
        data() {
            return {
                //背景图片
                note: {
                    backgroundImage: "url(" + require("../assets/images/login.jpg") + ")",
                    backgroundSize: "cover",
                    backgroundAttachment:"fixed",
                    height:"100%",
                    width:"100%",
                    margin: 0,
                    overflow: "hidden",
                    position:"absolute"
                },
                // 验证码
                captchaUrl:'',
                // 记住我
                checked:true,
                // 登录表单的数据
                loginForm:{
                   userName:'admin',
                   password:'admin',
                   code:'',
                   uuid:''
                },
                // 登录表单的数据的校验规则
                loginFormRules:{
                    userName:[{required:true, message:'请输入用户名', trigger:'blur'}],
                    password:[{required:true, message:'请输入密码', trigger:'blur'}],
                    code:[{required:true, message:'请输入验证码', trigger:'blur'}]
                }

            };
        },
        created() {
          this.getCaptcha()
        },
        methods: {
            //获取验证码
            getCaptcha(){
                getCaptchaImage().then(data=>{
                    this.captchaUrl = data.img
                    this.loginForm.uuid = data.uuid
                })
            },
            //登录
            submitLogin(){
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        login(this.loginForm).then(data=>{
                            if(data){
                                window.sessionStorage.setItem('token',data)
                                // 页面跳转
                                let path =  this.$route.query.redirect;
                                this.$router.replace((path == '/'||path==undefined?'home':path))
                            }
                        })
                    }
                });
            }
        },
    }
</script>
<style>
    .loginPage{
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 15px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }
    .loginTitle{
        margin: 0px auto 40px auto;
        text-align: center;
    }
    .loginRemember{
        text-align: left;
        margin: 0px 0px 15px 0px;
    }
    .el-form-item__content{
        display: flex;
        align-items: center;
    }
</style>

